#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "interface.h"

// 欢迎页面
void welcomeUI()
{
    system("clear");
    printf("\t\t--------------------------------------------------\n");
    printf("\t\t                                                 \n");
    printf("\t\t                                                 \n");
    printf("\t\t                欢迎使用本航班系统                  \n");
    printf("\t\t                                                 \n");
    printf("\t\t                                                 \n");
    printf("\t\t                                                 \n");
    printf("\t\t                                                 \n");
    printf("\t\t                                                 \n");
    printf("\t\t--------------------按回车键继续--------------------\n");
    printf("\t\t");
    getchar();
    system("clear");
    userUI();
}

// 用户界面
void userUI()
{

    // 用字符输入的原因是防止输入其他字符而导致程序奔溃
    char c;
    list_t *head = create_list();
    dclist_t *dchead = create_dclist();

    read_user_file(head);
    delete_tlist(head);

    read_flight_file(dchead);
    delete_tdclist(dchead);

    if (head == NULL)
    {
        perror("create_list failed\n");
        exit(1);
    }
    printf("\t\t--------------------------------------------------\n");
    printf("\t\t                  用户登录页面                     \n");
    printf("\t\t                                                 \n");
    printf("\t\t                  1.登陆用户                      \n");
    printf("\t\t                  2.注册用户                      \n");
    printf("\t\t                                                 \n");
    printf("\t\t                                                 \n");
    printf("\t\t-------------------请输入对应数字-------------------\n");
    printf("\t\t");
    scanf("%c", &c);
    // 清空缓存区，把换行符(\n)拿走
    getchar();
    if (c == '1')
    {
        // 清空当前终端页面//欢迎页面
        void welcomeUI();
        system("clear");
        loginUI(head, dchead);
    }
    else if (c == '2')
    {
        system("clear");
        enrollUI(head, dchead);
    }
    else
    {
        system("clear");
        loginUI(head, dchead);
    }
}

// 登录页面
void loginUI(list_t *head, dclist_t *dchead)
{

    char c;
    user_t user;

    printf("\t\t--------------------------------------------------\n");
    printf("\t\t                  请你登录                         \n");
    printf("\t\t                                                  \n");
    printf("\t\t                  用户名：                         \n");
    printf("\t\t");
    fgets(user.name, sizeof(user.name), stdin);
    user.name[strlen(user.name) - 1] = '\0';
    printf("\t\t                  密  码：                         \n");
    printf("\t\t");
    fgets(user.password, sizeof(user.password), stdin);
    user.password[strlen(user.password) - 1] = '\0';
    printf("\t\t                                                  \n");
    printf("\t\t                                                  \n");
    printf("\t\t-------------------请按回车登录----------------------\n");

    user_t *resUser = NULL;
    resUser = find_user(head, &user, cmpUserAll);

    printf("\t\t");
    getchar();

    if (resUser != NULL)
    {
        printf("\t\t登录成功\n");
        // 登录成功
        if (resUser->flag == 0)
        {
            system("clear");
            // 超级用户页面
            rootFlightUI(head, dchead);
        }
        else
        {
            system("clear");
            // 超级用户页面
            commonFlightUI(head, dchead);
        }
    }
    else
    {
        // 登录失败,账号或者密码不正确
        printf("\t\t登录失败,账号或者密码不正确,输入1跳转注册页面,输入其他不跳转页面\n");
        printf("\t\t");
        scanf("%c", &c);
        // 清空缓存区，把换行符(\n)拿走
        getchar();
        if (c == '1')
        {
            system("clear");
            // 注册页面
            enrollUI(head, dchead);
        }
        else
        {
            system("clear");
            // 登录页面
            loginUI(head, dchead);
        }
    }
}

// 用户注册页面
void enrollUI(list_t *head, dclist_t *dchead)
{
    int flag;
    user_t user;

    // 区分超级用户和普通用户
    if (head->next == NULL)
    {
        flag = 0;
    }
    else
    {
        flag = 1;
    }
    printf("\t\t--------------------------------------------------\n");
    printf("\t\t                 请你注册                         \n");
    printf("\t\t                                                 \n");

    printf("\t\t                 用户名:                          \n");
    printf("\t\t");
    fgets(user.name, sizeof(user.name), stdin);
    user.name[strlen(user.name) - 1] = '\0';

    printf("\t\t                 密 码:                           \n");
    printf("\t\t");
    fgets(user.password, sizeof(user.password), stdin);
    user.password[strlen(user.password) - 1] = '\0';

    printf("\t\t                                                 \n");
    printf("\t\t                                                 \n");
    printf("\t\t------------------按回车注册------------------------\n");

    user.flag = flag;
    int res = insert_user(head, &user);
    if (res == -1)
    {
        printf("\t\t注册失败,请重新填写用户名\n");
        enrollUI(head, dchead);
    }
    else
    {
        // 注册成功写入文件中
        save_oneUser_file(head);
        printf("\t\t注册成功,请按回车,进入登录页面进行登录\n");
        printf("\t\t");
        getchar();
        system("clear");
        loginUI(head, dchead);
    }
}

// 超级用户航班页面
void rootFlightUI(list_t *head, dclist_t *dchead)
{

    char c;

    printf("\t\t-----------------欢迎使用本航班系统------------------\n");
    printf("\t\t                                                  \n");
    printf("\t\t                1.显示全部航班                      \n");
    printf("\t\t                2.按照类型查询航班                  \n");
    printf("\t\t                3.按照起飞日期和票价排列显示航班       \n");
    printf("\t\t                4.根据起始地和目的地查询航班          \n");
    printf("\t\t                5.根据出发日期查询航班               \n");
    printf("\t\t                6.添加航班                         \n");
    printf("\t\t                7.删除航班                         \n");
    printf("\t\t                8.修改航班                         \n");
    printf("\t\t                9.切换用户                         \n");
    printf("\t\t                0.退出系统                         \n");
    printf("\t\t                                                  \n");
    printf("\t\t------------------请输入对应的数字-------------------\n");

    printf("\t\t");
    scanf("%c", &c);
    // 清空缓存区
    getchar();

    if (c == '1')
    {

        system("clear");
        print_flight(dchead, showAll);
        printf("\t\t已帮你查询完毕,按回车回到主页面\n");
        printf("\t\t");
        getchar();
        system("clear");
        rootFlightUI(head, dchead);
    }
    else if (c == '2')
    {

        system("clear");
        flight_t flight;
        char fltype[16];
        printf("\t\t请输入你要查询的航班类型:\n");
        printf("\t\t");
        fgets(flight.fltype, sizeof(flight.fltype), stdin);
        flight.fltype[strlen(flight.fltype) - 1] = '\0';

        find_flight_type(dchead, &flight, compareType, showAll);
        printf("\t\t已帮你查询完毕,按回车回到主页面\n");
        printf("\t\t");
        getchar();
        system("clear");
        rootFlightUI(head, dchead);
    }
    else if (c == '3')
    {

        system("clear");
        sort_flight_date_fare(dchead);
        printf("\t\t已帮你查询完毕,按回车回到主页面\n");
        printf("\t\t");
        getchar();
        system("clear");
        rootFlightUI(head, dchead);
    }
    else if (c == '4')
    {
        system("clear");

        flight_t flight;

        printf("\t\t请输入始发地:\n");
        printf("\t\t");
        fgets(flight.begaddr, sizeof(flight.begaddr), stdin);
        flight.begaddr[strlen(flight.begaddr) - 1] = '\0';

        printf("\t\t请输入终点站:\n");
        printf("\t\t");
        fgets(flight.endaddr, sizeof(flight.endaddr), stdin);
        flight.endaddr[strlen(flight.endaddr) - 1] = '\0';

        find_flight_begaddr_endaddr(dchead, &flight);

        printf("\t\t已帮你查询完毕,按回车回到主页面\n");
        printf("\t\t");
        getchar();
        system("clear");
        rootFlightUI(head, dchead);
    }
    else if (c == '5')
    {
        system("clear");

        flight_t flight;

        printf("\t\t请输入你要出发的日期(格式为yyyy-MM-dd,例如2023-01-09):\n");
        printf("\t\t");
        fgets(flight.fldate, sizeof(flight.fldate), stdin);
        flight.fldate[strlen(flight.fldate) - 1] = '\0';

        find_flight_fldate(dchead, &flight);

        printf("\t\t已帮你查询完毕,按回车回到主页面\n");
        printf("\t\t");
        getchar();
        system("clear");
        rootFlightUI(head, dchead);
    }
    else if (c == '6')
    {
        system("clear");
        insertFlightUI(head, dchead);
    }
    else if (c == '7')
    {
        flight_t flight = {0};

        int i = 0;
        system("clear");
        printf("\t\t请输入你要删除的航班号id:\n");
        printf("\t\t");
        fgets(flight.id, sizeof(flight.id), stdin);
        flight.id[strlen(flight.id) - 1] = '\0';
        i = delete_dclist(dchead, &flight, compareId);
        if (i == 0)
        {

            save_flight_file(dchead);
            printf("\t\t删除成功,请按回车继续\n");
            printf("\t\t");
            getchar();
            system("clear");
            rootFlightUI(head, dchead);
        }
        else
        {
            printf("\t\t系统忙,请稍后...\n");
            printf("\t\t按回车回到主页面\n");
            printf("\t\t");
            getchar();
            system("clear");
            rootFlightUI(head, dchead);
        }
    }
    else if (c == '8')
    {
        system("clear");
        updataFlightUI(head, dchead);
    }
    else if (c == '9')
    {
        system("clear");
        loginUI(head, dchead);
    }
    else if (c == '0')
    {
        save_user_file(head);
        exit(0);
    }
    else
    {
        system("clear");
        rootFlightUI(head, dchead);
    }
}

// 普通用户航班页面
void commonFlightUI(list_t *head, dclist_t *dchead)
{

    char c;
    printf("\t\t-----------------欢迎使用本航班系统------------------\n");
    printf("\t\t                                                 \n");
    printf("\t\t                                                 \n");
    printf("\t\t                1.显示全部航班                     \n");
    printf("\t\t                2.按照类型查询航班                  \n");
    printf("\t\t                3.按照起飞日期和票价排列显示航班       \n");
    printf("\t\t                4.根据起始地和目的地查询航班          \n");
    printf("\t\t                5.根据出发日期查询航班               \n");
    printf("\t\t                6.切换用户                         \n");
    printf("\t\t                7.退出系统                         \n");
    printf("\t\t                                                 \n");
    printf("\t\t                                                 \n");
    printf("\t\t------------------请输入对应的数字-------------------\n");

    printf("\t\t");
    scanf("%c", &c);
    // 清空缓存区
    getchar();

    if (c == '1')
    {
        system("clear");
        print_flight(dchead, showAll);
        printf("\t\t已帮你查询完毕,按回车回到主页面\n");
        printf("\t\t");
        getchar();
        system("clear");
        commonFlightUI(head, dchead);
    }
    else if (c == '2')
    {
        system("clear");
        flight_t flight;

        printf("\t\t请输入你要查询的航班类型:\n");
        printf("\t\t");
        fgets(flight.fltype, sizeof(flight.fltype), stdin);
        flight.fltype[strlen(flight.fltype) - 1] = '\0';

        find_flight_type(dchead, &flight, compareType, showAll);
        printf("\t\t已帮你查询完毕,按回车回到主页面\n");
        printf("\t\t");
        getchar();
        system("clear");
        commonFlightUI(head, dchead);
    }
    else if (c == '3')
    {
        system("clear");
        sort_flight_date_fare(dchead);
        printf("\t\t已帮你查询完毕,按回车回到主页面\n");
        printf("\t\t");
        getchar();
        system("clear");
        commonFlightUI(head, dchead);
    }
    else if (c == '4')
    {
        system("clear");

        flight_t flight;

        printf("\t\t请输入始发地:\n");
        printf("\t\t");
        fgets(flight.begaddr, sizeof(flight.begaddr), stdin);
        flight.begaddr[strlen(flight.begaddr) - 1] = '\0';

        printf("\t\t请输入终点站:\n");
        printf("\t\t");
        fgets(flight.endaddr, sizeof(flight.endaddr), stdin);
        flight.endaddr[strlen(flight.endaddr) - 1] = '\0';

        find_flight_begaddr_endaddr(dchead, &flight);

        printf("\t\t已帮你查询完毕,按回车回到主页面\n");
        printf("\t\t");
        getchar();
        system("clear");
        commonFlightUI(head, dchead);
    }
    else if (c == '5')
    {
        system("clear");

        flight_t flight;

        printf("\t\t请输入你要出发的日期(格式为yyyy-MM-dd,例如2023-01-09):\n");
        printf("\t\t");
        fgets(flight.fldate, sizeof(flight.fldate), stdin);
        flight.fldate[strlen(flight.fldate) - 1] = '\0';

        find_flight_fldate(dchead, &flight);

        printf("\t\t已帮你查询完毕,按回车回到主页面\n");
        printf("\t\t");
        getchar();
        system("clear");
        commonFlightUI(head, dchead);
    }
    else if (c == '6')
    {
        system("clear");
        loginUI(head, dchead);
    }
    else if (c == '7')
    {
        save_user_file(head);
        exit(0);
    }
    else
    {
        system("clear");
        commonFlightUI(head, dchead);
    }
}

// 添加航班页面
void insertFlightUI(list_t *head, dclist_t *dchead)
{

    char ch[8];
    flight_t flight;
    printf("\t\t请输入航班号:\n");
    printf("\t\t");
    fgets(flight.id, sizeof(flight.id), stdin);
    flight.id[strlen(flight.id) - 1] = '\0';

    printf("\t\t请输入起始站:\n");
    printf("\t\t");
    fgets(flight.begaddr, sizeof(flight.begaddr), stdin);
    flight.begaddr[strlen(flight.begaddr) - 1] = '\0';

    printf("\t\t请输入终点站:\n");
    printf("\t\t");
    fgets(flight.endaddr, sizeof(flight.endaddr), stdin);
    flight.endaddr[strlen(flight.endaddr) - 1] = '\0';

    printf("\t\t请输入飞机起飞日期(格式为yyyy-MM-dd,例如2023-01-09):\n");
    printf("\t\t");
    fgets(flight.fldate, sizeof(flight.fldate), stdin);
    flight.fldate[strlen(flight.fldate) - 1] = '\0';

    printf("\t\t请输入飞机类型:\n");
    printf("\t\t");
    fgets(flight.fltype, sizeof(flight.fltype), stdin);
    flight.fltype[strlen(flight.fltype) - 1] = '\0';

    printf("\t\t请输入飞机起飞时间(格式为HH:mm,例如09:01 20:10):\n");
    printf("\t\t");
    fgets(flight.begtime, sizeof(flight.begtime), stdin);
    flight.begtime[strlen(flight.begtime) - 1] = '\0';

    printf("\t\t请输入飞机降落时间(格式为HH:mm,例如09:01 20:10):\n");
    printf("\t\t");
    fgets(flight.endtime, sizeof(flight.endtime), stdin);
    flight.endtime[strlen(flight.endtime) - 1] = '\0';

    printf("\t\t请输入飞机的票价:\n");
    printf("\t\t");
    scanf("%s", ch);
    getchar();

    float fare = atoi(ch);
    flight.fare = fare;

    int res = -2;
    char c;
    res = insert_flight(dchead, &flight);
    if (res == 0)
    {
        write_flight_file(dchead);
        system("clear");
        printf("\t\t添加成功,请按回车返回用户主页面\n");
        printf("\t\t");
        getchar();
        system("clear");
        rootFlightUI(head, dchead);
    }
    else
    {
        system("clear");
        printf("\t\t添加失败,系统忙,请稍后...\n");
        printf("\t\t输入1返回主页面,输入其他则重新添加\n");
        printf("\t\t");
        scanf("%c", &c);
        getchar();
        if (c == '1')
        {
            system("clear");
            rootFlightUI(head, dchead);
        }
        else
        {
            system("clear");
            insertFlightUI(head, dchead);
        }
    }
}

// 修改航班页面
void updataFlightUI(list_t *head, dclist_t *dchead)
{

    flight_t flight;

    char c;
    printf("\t\t请输入要修改的航班号:\n");
    printf("\t\t");
    fgets(flight.id, sizeof(flight.id), stdin);
    flight.id[strlen(flight.id) - 1] = '\0';

    printf("\t\t请输入航班起飞时间(格式为HH:mm,例如09:01 20:10):\n");
    printf("\t\t");
    fgets(flight.begtime, sizeof(flight.begtime), stdin);
    flight.begtime[strlen(flight.begtime) - 1] = '\0';

    printf("\t\t请输入航班降落时间(格式为HH:mm,例如09:01 20:10):\n");
    printf("\t\t");
    fgets(flight.endtime, sizeof(flight.endtime), stdin);
    flight.endtime[strlen(flight.endtime) - 1] = '\0';

    if (!updata_dclist(dchead, &flight, compareId))
    {
        save_flight_file(dchead);
        printf("\t\t修改成功,请按回车回到主页面");
        getchar();
        system("clear");
        rootFlightUI(head, dchead);
    }
    else
    {
        printf("\t\t系统忙,请稍后...\n");
        printf("\t\t输入1回到主页面,其他回车不跳转\n");
        printf("\t\t");
        scanf("%c", &c);
        getchar();
        if (c == '1')
        {
            system("clear");
            rootFlightUI(head, dchead);
        }
        else
        {
            system("clear");
            updataFlightUI(head, dchead);
        }
    }
}